import { loginByUsername, logout, getUserInfo } from '@/api/login'
import { Notify } from 'vue-ydui/dist/lib.rem/dialog'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { getShopInfo } from '../../api/system'

const user = {
  state: {
    user: '',
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    shopInfo: {},
    shopId: 0,
    account: {}
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_SHOP_INFO: (state, shopInfo) => {
      state.shopInfo = shopInfo
    },
    SET_SHOP_ID: (state, shopId) => {
      state.shopId = shopId
    },
    MY_ACCOUNT: (state, account) => {
      state.account = account
    }
  },

  actions: {
    // 用户名登录
    LoginByUsername({ commit }, userInfo) {
      userInfo.username = userInfo.username.trim()
      return new Promise((resolve, reject) => { // 只是new了Promise对象,并没有调用，但它里面的函数就执行了
        console.log(userInfo)
        loginByUsername(userInfo).then(res => {
          const data = res.data
          if (data.status_code === 200) {
            commit('SET_TOKEN', data.data.access_token) // 存下返回的token
            setToken(res.data.data.access_token)
          } else {
            Notify({
              mes: data.message,
              timeout: 3000
            })
          }
          resolve(res) // Promise参数resolve(成功)表示异步函数操作成功
        }).catch(error => {
          reject(error) // Promise参数reject(失败)表示异步函数失败操作
        })
      })
    },

    // 获取商户信息
    GetShopInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getShopInfo(state.shopId).then((res) => {
          if (res.data.status_code === 200) {
            commit('SET_SHOP_INFO', res.data.data)
            commit('SET_SHOP_ID', res.data.data.shop_id)
            localStorage.setItem('shop_id', res.data.data.shop_id)
            resolve(res)
          } else {
            reject(res.data.data.message)
          }
        })
      })
    },

    // 获取用户信息
    GetUserInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getUserInfo({ token: getToken() }).then(response => {
          const data = response.data
          if (data.status_code === 200) {
            commit('SET_ROLES', data.data.roles)
            commit('SET_NAME', data.data.name)
            commit('SET_SHOP_ID', data.data.shop_id)
            commit('MY_ACCOUNT', data.data)
            commit('SET_AVATAR', data.data.head_image)
            // commit('SET_INTRODUCTION', data.data.introduction)
          } else {
            Notify({
              mes: data.message,
              timeout: 3000
            })
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
